<template>
  <PageLayout class="form-container">
    <a-card>
      <div class="form-title">{{ route.query.id ? "编辑" : "新增" }}广告</div>
      <div class="form-box w-600">
        <a-form
          :model="formState"
          :rules="rules"
          ref="formRef"
          layout="Vertical"
          :labelCol="{ style: { width: '80px' } }"
        >
          <a-form-item label="标题" name="title" required>
            <a-input
              v-model:value="formState.title"
              placeholder="请输入标题"
              :maxlength="50"
            />
          </a-form-item>
          <a-form-item label="广告图" name="imageUrl" required>
         
            <UploadImg
              v-model:imageUrl="formState.imageUrl"
            ></UploadImg>
          </a-form-item>
          <a-form-item label="url链接" name="linkUrl" required>
            <a-input
              v-model:value="formState.linkUrl"
              placeholder="请输入url链接"
              :maxlength="50"
            />
          </a-form-item>
          <a-button
            type="primary"
            class="float-right"
            style="width: 100px"
            @click="save"
            >确定</a-button
          >
        </a-form>
      </div>
    </a-card>
  </PageLayout>
</template>

<script setup>
import { postBannerAdd, postBannerUpdate, postBannerDetail } from "@/api/common";
import { useStoreCommonEnum } from "@/store";
import { message } from "ant-design-vue";
import { onMounted } from "vue";
const storeCommonEnum = useStoreCommonEnum();

const route = useRoute();
const router = useRouter();
const formRef = ref(null);

const formState = reactive({
  id: "",
  title: "",
  imageUrl: "",
  linkUrl: "",
});
const deptOptions = ref([]);

onMounted(async () => {
  if (route.query.id) {
    postBannerDetail({ id: route.query.id }).then((res) => {
      for (let key in formState) {
        formState[key] = res.data[key];
      }
    });
  }
});

const rules = {
  title: [{ required: true, message: "请输入标题", trigger: "blur" }],
  imageUrl: [{ required: true, message: "请上传广告图", }],
  linkUrl: [{ required: true, message: "请输入链接", trigger: "blur" }],
  // deptId: [{ required: true, message: "请选择部门", trigger: "blur" }],
};

const save = () => {
  console.log('formState',formState);
  formRef.value.validate().then(() => {
    const requestUrl = formState.id ? postBannerUpdate : postBannerAdd;
    requestUrl({ ...formState }).then(() => {
      message.success(formState.id ? "编辑成功" : "新增成功");
      router.go(-1);
    });
  });
};
</script>

<style lang="less" scoped>
.btn-box {
  margin-top: 24px;
  margin-left: 130px;
  :deep(.ant-btn) {
    margin-right: 16px;
  }
}
</style>
